package com.zds.scm.dao.impl;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.zds.scm.common.dao.AbstractDAOImpl;
import com.zds.scm.common.dao.DaoException;
import com.zds.scm.dao.CourseInfoDAO;
import com.zds.scm.domain.CourseInfo;
import com.zds.scm.domain.DeptInfo;

public class CourseInfoDAOImpl extends AbstractDAOImpl<CourseInfo> implements CourseInfoDAO {
	@Override
	protected CourseInfo rowMapper(ResultSet rs) throws SQLException {
		CourseInfo courseInfo=new CourseInfo();
		try {
			DeptInfo deptInfo=new DeptInfo();
			deptInfo.setDeptCode(rs.getString("Dept_Code"));
			deptInfo.setDeptName(rs.getString("dept_name"));
			deptInfo.setDeptDesc(rs.getString("dept_desc"));
			courseInfo.setDeptInfo(deptInfo);
			courseInfo.setCourseCode(rs.getString("Course_Code"));
			courseInfo.setCourseName(rs.getString("Course_Name"));
			courseInfo.setCourseCredit(rs.getInt("Course_Credit"));
			courseInfo.setCourseLimit(rs.getInt("Course_Limit"));
			courseInfo.setCourseType(rs.getString("Course_Type"));
		} catch (Exception e) {//在DAO层不处理异常,直接抛出到外层
			   logger.error("DAO层结果集映射处理错误:"+e.getMessage());
			   throw new DaoException(e.getMessage(),e);
		}
		return courseInfo;
	}
	@Override
	public boolean delete(CourseInfo courseInfo) {
		String sql="delete from courseinfo where course_code=?";
		Object[] args=new Object[]{courseInfo.getCourseCode()};
 		return super.updateEntity(sql, args)>0;
	}
	@Override
	public boolean insert(CourseInfo courseInfo) {
		String sql="insert into courseinfo(Course_Code,Course_Name,Dept_Code,Course_Credit,Course_Limit,Course_Type) values(?,?,?,?,?,?)";
		Object[] args=new Object[]{courseInfo.getCourseCode(),courseInfo.getCourseName(),
				                   courseInfo.getDeptInfo().getDeptCode(),  courseInfo.getCourseCredit(),
				                   courseInfo.getCourseLimit(),courseInfo.getCourseType()};
 		return super.updateEntity(sql, args)>0;
	}

	@Override
	public boolean update(CourseInfo courseInfo) {
		String sql="update courseinfo set Course_Name=?,Dept_Code=?,Course_Credit=?,Course_Limit=?,Course_Type=? where Course_Code=?";
		Object[] args=new Object[]{courseInfo.getCourseName(),courseInfo.getDeptInfo().getDeptCode(),  
				                   courseInfo.getCourseCredit(), courseInfo.getCourseLimit(),
				                   courseInfo.getCourseType(),courseInfo.getCourseCode()};
 		return super.updateEntity(sql, args)>0;
	}
	@Override
	public List<CourseInfo> findAll() {
		return findAll(-1,-1);
	}
	@Override
	public List<CourseInfo> findAll(int currentPage, int pageSize){
		String sql="select a.*,b.dept_name,b.dept_desc from courseinfo a,deptInfo b where a.dept_code=b.dept_code";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{(currentPage-1)*pageSize,pageSize};
		}
 		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRows(){
		String sql="select count(*) from courseInfo";
		Object[] args=new Object[]{};
		return super.findEntityCount(sql, args);
	}
	@Override
	public CourseInfo findById(Serializable id) {
		String sql="select a.*,b.dept_Name,b.dept_desc from courseinfo a,deptInfo b where a.dept_code=b.dept_code and a.course_code=?";
		Object[] args=new Object[]{id};
		return super.findEntity(sql, args);
	}
	@Override
	public List<CourseInfo> findByDeptCode(String DeptCode,int currentPage, int pageSize) {
		String sql="select a.*,b.dept_name,b.dept_desc from courseinfo a,deptInfo b where a.dept_code=b.dept_code and a.dept_code=?";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{DeptCode};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{DeptCode,(currentPage-1)*pageSize,pageSize};
		}
 		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRowsDeptCode(String DeptCode){
		String sql="select count(*) from courseInfo where dept_code=?";
		Object[] args=new Object[]{DeptCode};
		return super.findEntityCount(sql, args);
	}
	@Override
	public List<CourseInfo> findByName(String likeCourseName,int currentPage, int pageSize) {
		String sql="select a.*,b.dept_name,b.dept_desc from courseinfo a,deptinfo b where a.dept_code=b.dept_code and a.Course_Name like ?";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{"%"+likeCourseName+"%"};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{"%"+likeCourseName+"%",(currentPage-1)*pageSize,pageSize};
		}
 		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRowsByName(String likeCourseName){
		String sql="select count(*) from courseInfo where course_Name like ?";
		Object[] args=new Object[]{"%"+likeCourseName+"%"};
		return super.findEntityCount(sql, args);
	}
	@Override
	public List<CourseInfo> findByCode(String likeCourseCode,int currentPage, int pageSize) {
		String sql="select a.*,b.dept_name,b.dept_desc from courseinfo a,deptInfo b where a.dept_code=b.dept_code a.Course_code like ?";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{"%"+likeCourseCode+"%"};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{"%"+likeCourseCode+"%",(currentPage-1)*pageSize,pageSize};
		}
 		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRowsByCode(String likeCourseCode){
		String sql="select count(*) from courseInfo where course_code like ?";
		Object[] args=new Object[]{"%"+likeCourseCode+"%"};
		return super.findEntityCount(sql, args);
	}
	@Override
	public List<CourseInfo> findByType(String courseType,int currentPage, int pageSize) {
		String sql="select a.*,b.dept_name,b.dept_desc from courseinfo a,deptInfo b where a.dept_code=b.dept_code and a.Course_type = ?";
		Object[] args=null;
		if(currentPage<1||pageSize<1){
			args=new Object[]{courseType};
		}else {
			sql+=" limit ?,?";
			args=new Object[]{courseType,(currentPage-1)*pageSize,pageSize};
		}
 		return super.findEntitys(sql, args);
	}
	@Override
	public int getTotalRowsByType(String courseType){
		String sql="select count(*) from courseInfo where Course_type = ?";
		Object[] args=new Object[]{courseType};
		return super.findEntityCount(sql, args);
	}
}
